﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E.Web.UI.Page;
using X.Common;
using E.Enum;
using E.BLL;
using E.Model;
using X.Pager;
using X.Pager.List;
using System.Text;

namespace E.Web.Teacher.report.exam
{
    public partial class AverageScoreByClassReport : TeacherBasePage
    {

        public Dictionary<int, string> certificateList;
        public int currentCertificateID;//项目ID
        public int courseid;//课程ID
        public IList<AverageStaticsModel> staticsList;


        public string act;

        protected override void ShowPage()
        {
            ExamBLL bll = new ExamBLL();
            act = XRequest.GetString("act");

            switch (authority)
            {
                case EnumTeacherAuthority.Internals:
                    certificateList = (new TeacherBLL()).GetTeacherProjectInfo(teacherid);
                    break;
                case EnumTeacherAuthority.Normal:
                    certificateList = (new CourseBLL()).GetCertificateList();
                    break;
                case EnumTeacherAuthority.Admin:
                    certificateList = (new CourseBLL()).GetAllCertificateAndProjectList();
                    break;
                default: break;
            }

            //relationid = XRequest.GetInt("RelationSlt", -1);

            courseid = XRequest.GetInt("CourseSlt", -1);
            currentCertificateID = XRequest.GetInt("CertificateSlt", -1);
            areaid = XRequest.GetInt("AreaSlt", -1);


            if (ispost || act == "s" || act == "d")
            {

                SearchExamModel searchModel = new SearchExamModel();
                searchModel.CertificateID = currentCertificateID;
                searchModel.CourseCertificateID = courseid;

                AverageStaticsListModel listModel = new AverageStaticsListModel();

                CheckPageIndexValue(XRequest.GetInt("s", 1) - 1, XRequest.GetInt("g", 0), XRequest.GetInt("z", customerPageSize));

                if (act == "s")
                {
                    listModel = bll.GetAverageScoreStaticListModelByCondition(searchModel, customerPageSize, pageIndex);
                    if (listModel.TotalCount > 0)
                    {
                        count = listModel.TotalCount;
                        staticsList = listModel.List;
                        PagerBase pager = new ListPager(count, pageIndex, currentPgGroup, customerPageSize, defaultPagesInGroup);

                        Dictionary<string, string> parms = new Dictionary<string, string>();
                        parms.Add("act", act);
                        parms.Add("CertificateSlt", currentCertificateID.ToString());
                        parms.Add("CourseSlt", courseid.ToString());
                        //parms.Add("RelationSlt", relationid.ToString());
                        mypager = pager.DrawPager("selfpager", parms);
                    }
                }
                if (act == "d")
                {
                    listModel = bll.GetAverageScoreStaticListModelByCondition(searchModel, int.MaxValue, 0);
                    StringBuilder sb = ExportTable(listModel.List);
                    ExportData(sb);
                }
            }

        }

        private void ExportData(System.Text.StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            //System.Web.HttpContext.Current.Response.Charset = "GB2312";
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            //System.Web.HttpContext.Current.e = false;
            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
        private StringBuilder ExportTable(IList<AverageStaticsModel> list)
        {
            StringBuilder sb = new StringBuilder("");
            sb.Append("<table>\r\n");
            sb.Append("  <tr>\r\n");
            sb.Append("    <td style=\"border:solid 1px; font-weight:bolder;font:13px '宋体';\">课程名称</td>\r\n");
            sb.Append("    <td style=\"border:solid 1px; font-weight:bolder;font:13px '宋体';\">班级名称</td>\r\n");
            sb.Append("    <td style=\"border:solid 1px; font-weight:bolder;font:13px '宋体';\">学生数量</td>\r\n");
            sb.Append("    <td style=\"border:solid 1px; font-weight:bolder;font:13px '宋体';\">平均分</td>\r\n");
            sb.Append("  </tr>\r\n");
            foreach (AverageStaticsModel model in list)
            {
                sb.Append("  <tr>\r\n");
                sb.AppendFormat("    <td style=\"border:solid 1px\">{0}</td>\r\n", model.CourseName);
                sb.AppendFormat("    <td style=\"border:solid 1px\">{0}</td>\r\n", model.ClassCode);
                sb.AppendFormat("    <td style=\"border:solid 1px\">{0}</td>\r\n", model.StudentNumber);
                sb.AppendFormat("    <td style=\"border:solid 1px\">{0}</td>\r\n", model.AverageScore);

                sb.Append("  </tr>\r\n");
            }
            sb.Append("</table>\r\n<p>&nbsp;</p>\r\n");
            Alert(sb.ToString());
            return sb;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}